package com.woniu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.BillingRule;
import com.woniu.service.BillingRuleService;
import com.woniu.mapper.BillingRuleMapper;
import com.woniu.vo.BillingRuleVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Apricity
* @description 针对表【billing_rule(计费规则表)】的数据库操作Service实现
* @createDate 2025-02-13 20:16:58
*/
@Service
public class BillingRuleServiceImpl extends ServiceImpl<BillingRuleMapper, BillingRule>
    implements BillingRuleService {

    @Resource
    private BillingRuleMapper billingRuleMapper;

    @Override
    public List<BillingRuleVO> getBillingRuleList(String keyword, Integer pageNum, Integer pageSize) {
        Page<BillingRuleVO> page = new Page<>(pageNum, pageSize);
        List<BillingRuleVO> rules;
        
        if (keyword != null && !keyword.trim().isEmpty()) {
            rules = billingRuleMapper.queryBillingRuleByKeyword(keyword, page);
        } else {
            rules = billingRuleMapper.queryBillingRuleList(page);
        }
        
        return rules;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addBillingRule(BillingRule billingRule) {
        return billingRuleMapper.insert(billingRule) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBillingRule(Long id) {
        return billingRuleMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBillingRule(BillingRule billingRule) {
        if (billingRule.getId() == null) {
            return false;
        }
        return billingRuleMapper.updateBillingRule(billingRule) > 0;
    }
}




